package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.tool.utils.Func;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author longwenchao
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    @Lazy
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        IPage<AttrGroupEntity> page = page(new Query<AttrGroupEntity>().getPage(params),
                Wrappers.<AttrGroupEntity>lambdaQuery()
                        .eq(catelogId != 0, AttrGroupEntity::getCatelogId, catelogId)
                        .and(Func.isNotEmpty(key), i -> i.eq(AttrGroupEntity::getAttrGroupId, key)
                                .or()
                                .like(AttrGroupEntity::getAttrGroupName, key)
                        )
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 根据分类id查出所有的分组以及这些组里面的信息

        // 1. 查询分组信息

        List<AttrGroupEntity> entityList = list(Wrappers.<AttrGroupEntity>lambdaQuery()
                .eq(AttrGroupEntity::getCatelogId, catelogId)
        );

        return entityList.stream()
                .map(group -> {
                    AttrGroupWithAttrsVO vo = new AttrGroupWithAttrsVO();

                    BeanUtils.copyProperties(group, vo);

                    List<AttrEntity> attrs = attrService.getAttrRelation(group.getAttrGroupId());

                    vo.setAttrs(attrs);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroupVO> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

}
